home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML ConsoleMax.sea / XML ConsoleMax / Required / ccs_util.jar / com / commerceone / util / io / ReaderPair.class (.txt) < prev   
Encoding:
Java Class File  |  1999-12-09  |  1.5 KB  |  69 lines

  1. package com.commerceone.util.io;
  2.  
  3. import com.commerceone.util.contract.Contract;
  4. import java.io.IOException;
  5. import java.io.Reader;
  6.  
  7. public class ReaderPair extends Reader {
  8.    private Reader begin;
  9.    private final Reader end;
  10.  
  11.    public void close() throws IOException {
  12.       try {
  13.          if (this.begin != null) {
  14.             this.begin.close();
  15.          }
  16.       } finally {
  17.          this.end.close();
  18.       }
  19.  
  20.    }
  21.  
  22.    public ReaderPair(Reader car, Reader cdr) {
  23.       Contract.require(car != null && cdr != null);
  24.       this.begin = car;
  25.       this.end = cdr;
  26.    }
  27.  
  28.    public int read(char[] cbuf, int off, int len) throws IOException {
  29.       int charsRead = 0;
  30.       if (this.begin != null) {
  31.          charsRead = this.begin.read(cbuf, off, len);
  32.       }
  33.  
  34.       if (charsRead < 0) {
  35.          this.finishBegin();
  36.          charsRead = 0;
  37.       }
  38.  
  39.       if (charsRead < len) {
  40.          int result = this.end.read(cbuf, off + charsRead, len - charsRead);
  41.          return result < 0 ? result : result + charsRead;
  42.       } else {
  43.          return charsRead;
  44.       }
  45.    }
  46.  
  47.    public int read() throws IOException {
  48.       if (this.begin != null) {
  49.          int charRead = this.begin.read();
  50.          if (charRead > -1) {
  51.             return charRead;
  52.          }
  53.  
  54.          this.finishBegin();
  55.       }
  56.  
  57.       return this.end.read();
  58.    }
  59.  
  60.    private void finishBegin() {
  61.       try {
  62.          this.begin.close();
  63.       } catch (IOException var2) {
  64.       }
  65.  
  66.       this.begin = null;
  67.    }
  68. }
  69.